#include "hd.h"
#include "status.h"
void port_init(void)
{
    PORTB = 0b00010011;
    DDRB  = 0b00101100;
    PORTC = 0b00001000; //m103 output only
    DDRC  = 0b00000111;
    PORTD = 0b11000100;
    DDRD  = 0b11010011;
}

void adc_init(void)
{
    ADCSRA = 0x00; //disable adc
    ADMUX = 0b11000101;  //select adc input 0
    ACSR  = 0x80;
    ADCSRA = 0x8C;
// DIDR0|=0b00100000;
}

ISR(SIG_ADC) //void adc_isr(void)
{
 //conversion complete, read value (int) using...
 // value=ADCL;            //Read 8 low bits first (important)
 // value|=(int)ADCH << 8; //read 2 high bits and shift into top byte

    INSERT_ADC_ISR_CODE;
}

//TIMER0 initialize - prescale:64
// desired value: 100Hz
// actual value: 100.160Hz (0.2%)
void timer0_init(void)
{
    TCCR0 = 0x00; //stop
    TCNT0 = 0x64; //set count
    TCCR0 = 0x03; //start timer
}

ISR(SIG_OVERFLOW0)
{

	TCNT0 = 0xB2; //reload counter value
//	ISR_TIM0_CODE;
    //Buffer_T[2] = 0x0f;
    //Buffer_T[3] = value.valueGas >> 8;
    //Buffer_T[4] = value.valueGas;
    rf_2();
    //rf_status();
    //CE_on;
    core();
}

//TIMER1 initialize - prescale:8
// WGM: 14) PWM fast, TOP=ICRn
// desired value: 100Hz
// actual value:
//top 1000
/*void timer1_init(void)
{
    TCCR1B = 0x00; //stop
    TCNT1H = 0x00; //setup
    TCNT1L = 0x00;
    OCR1AH = 0x01;
    OCR1AL = 0xff;
    OCR1BH = 0x01; // compare 500
    OCR1BL = 0xFF;
    ICR1H  = 0x03; //TOP 1000
    ICR1L  = 0xff;
    TCCR1A = 0x33;
    TCCR1B = 0x0A; //start Timer clk/8
}*/

/*ISR(SIG_OVERFLOW1)
{
	OCR1BH = ocr1bh; // compare 500
	OCR1BL = ocr1bl;
}*/

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
/*void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x02;
 UCSRC = BIT(URSEL) | 0x06;
 UBRRL = 0x0C; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x18;
}*/

//TIMER2 initialize - prescale:1
// WGM: Normal
// desired value: 100000Hz
// actual value: 100000.000Hz (0.0%)
/*void timer2_init(void)
{
 TCCR2 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT2 = 0xF6; //setup
 OCR2  = 0x0A;
 //TCCR2 = 0x01; //start
}

ISR(SIG_OVERFLOW2)
{
	TCNT2 = 0xF6; //reload counter value
    time2Flag = 0x00;
}*/

void nRF24L01_Initial(void)
{

	CE_off;			// chip enable
	CSN_on;			// Spi disable
	MSCK_off;			// Spi clock line init high
	rf_initial();
}

ISR(SIG_INTERRUPT0)
{
    //rf_interrupt();
    rf_in1();
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
    cli(); //disable all interrupts
    port_init();
    adc_init();
    timer0_init();
//    timer1_init();
    //timer2_init();
 //uart0_init();
    nRF24L01_Initial();
    //MCUCR = 0x00;
    MCUCR = 0b00010000;//0b10010000;
    MCUCR |= 0b00000010; // 下降沿引脚中断
    GICR  = 0x00;
    GICR |= 0b01000000;//RF-INT0µÍµçÆ½ÖÐ¶Ï
    TIMSK = 0x01; //timer interrupt sources
    sei(); //re-enable interrupts
 //all peripherals are now initialized
}
